package com.taoyibao.hongcanting.ui.home.views;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.BaseRelativeLayout;
import com.taoyibao.hongcanting.model.DishModel;
import com.taoyibao.hongcanting.model.FoodTypeModel;
import com.taoyibao.hongcanting.utils.LogUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description: 购物车列表的item
 * @date :${DATA} 18:07
 */
public class ItemCartArea extends BaseRelativeLayout<FoodTypeModel> {
    private RecyclerView mRecyclerView;
    /**
     * 分类名称
     */
    private TextView mTvFoodTypeName;
    /**
     * 当前分类下总数量
     */
    private TextView mTvTotalCount;
    /**
     * 是否显示数量控制器 默认显示
     */
    private boolean isShow = true;
    private List<DishModel> mData;


    public ItemCartArea(Context context) {
        super(context);
    }

    public ItemCartArea(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemCartArea(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        mTvFoodTypeName = findViewById(R.id.tv_itemCartList_foodTypeName);
        mTvTotalCount = findViewById(R.id.tv_itemCartList_totleCount);
        mRecyclerView = findViewById(R.id.rv_shoppingCart_listitem);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mData = new ArrayList<>();
    }

    @Override
    public void setData(FoodTypeModel data) {
        LogUtils.dNormal("购物车一级",data.toString());
        mRecyclerView.setAdapter(new CommonAdapter<DishModel>(getContext(), R.layout.item_shopping_cart_secondary_shell, data.children) {
            @Override
            protected void convert(ViewHolder holder, DishModel dishModel, int position) {
                SecondaryItemCartArea secondaryItemCartArea = (SecondaryItemCartArea) holder.getConvertView();
                secondaryItemCartArea.setData(dishModel);
                secondaryItemCartArea.isShowCountController(isShow);
            }
        });
        if (!TextUtils.isEmpty(data.class_name)) {
            mTvFoodTypeName.setText(data.class_name);
        }
        mTvTotalCount.setText("(" + data.class_count + ")");
    }

    /**
     * 设置是否显示数量控制器
     *
     * @param show true 显示（默认显示）  false 不显示
     */
    public void isShowCountController(boolean show) {
        isShow = show;
    }

    @Override
    protected int bindLayout() {
        return R.layout.item_cart_list_area;
    }
}
